package com.john.pplay.activity;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;

import com.john.pplay.R;
import com.john.pplay.util.Setting;

public class SettingActivity extends BaseActivity implements View.OnClickListener {

    private Switch fit,real,full,hard,soft;
    private Setting setting;
    private TextView version;

    @Override
    protected void onCreate(Bundle bundle){
        super.onCreate(bundle);
        this.setContentView(R.layout.activity_setting);
        fit = (Switch)findViewById(R.id.switch_fit);
        real = (Switch)findViewById(R.id.switch_real);
        full = (Switch)findViewById(R.id.switch_full);
        hard = (Switch)findViewById(R.id.switch_hard);
        soft = (Switch)findViewById(R.id.switch_soft);
        version = (TextView)findViewById(R.id.tv_version);
        fit.setOnClickListener(this);
        real.setOnClickListener(this);
        full.setOnClickListener(this);
        hard.setOnClickListener(this);
        soft.setOnClickListener(this);
        init();
    }

    @Override
    protected void onPause(){
        if (fit.isChecked())
            setting.setMeasureType(Setting.FIT);
        if (real.isChecked())
            setting.setMeasureType(Setting.REAL);
        if (full.isChecked())
            setting.setMeasureType(Setting.FULL);
        if (hard.isChecked())
            setting.setDecodeType(Setting.HARDDECODE);
        if (soft.isChecked())
            setting.setDecodeType(Setting.SOFTDECODE);
        setting.save();
        super.onPause();
    }

    private void init(){
        setting = Setting.getInstance();
        switch (setting.getMeasureType()){
            case Setting.FIT:
                fit.setChecked(true);
                break;
            case Setting.REAL:
                real.setChecked(true);
                break;
            case Setting.FULL:
                full.setChecked(true);
                break;
        }

        switch (setting.getDecodeType()){
            case Setting.HARDDECODE:
                hard.setChecked(true);
                break;
            case Setting.SOFTDECODE:
                soft.setChecked(true);
                break;
        }

        String versionName = "";
        try {
            versionName = this.getPackageManager().getPackageInfo(this.getPackageName(),0).versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        version.setText(versionName);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.switch_fit:
                real.setChecked(false);
                full.setChecked(false);
                break;
            case R.id.switch_real:
                fit.setChecked(false);
                full.setChecked(false);
                break;
            case R.id.switch_full:
                fit.setChecked(false);
                real.setChecked(false);
                break;
            case R.id.switch_hard:
                soft.setChecked(false);
                break;
            case R.id.switch_soft:
                hard.setChecked(false);
                break;
        }
    }
}
